#!/usr/local/python27
# coding=utf8

import time

import mockserver
from mockserver.config import argument_parser
from mockserver.utils.config_parser import MockConfigParser
from mockserver.core import Mock
from mockserver.core.routes import RoutesMgr
from mockserver.utils.log import logger
from mockserver.utils.path_parser import PATH


mockfile =PATH.mockfile_path

def main():

    no_watch = argument_parser()

    logger.critical("start mockserver...")
    logger.critical("mockserver version %s " % mockserver.__version__)
    time.sleep(1)

    # parser mockfile
    config_server = MockConfigParser.do(mockfile)

    # routes
    if config_server:
        route_list = RoutesMgr.get_routes(config_server)
        
        logger.critical( "ROUTES:%s" % (str(route_list),))
        # new mock
        p_mock = Mock(auto_reload=not no_watch, port=config_server.port)
    else:
        route_list = []
        # new mock
        p_mock = Mock(auto_reload=not no_watch)

    p_mock.add_watch(mockfile)
    p_mock.add_handler(route_list)

    # start
    try:
        p_mock.start()
    except KeyboardInterrupt:
        pass


if __name__ == "__main__":
    main()


